package service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.User;
import util.Sql;

import java.util.List;

public class UserService {

    public List<User> selectuserAll(){//查询所有用户
        SqlSession sqlSession = Sql.openSeeion();
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.selectuserAll();
        sqlSession.close();
        return users;
    }

    public User selectById(int id){//按照id查询
        SqlSession sqlSession = Sql.openSeeion();
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        User user = userMapper.selectById(id);
        sqlSession.close();
        return user;
    }

    public int updateplus(User user){//动态修改
        SqlSession sqlSession = Sql.openSeeion();
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        int count=userMapper.updateplus(user);
        sqlSession.commit();
        sqlSession.close();
        return count;
    }
    public User insertoff(String usersname){//查询重名
        SqlSession sqlSession = Sql.openSeeion();
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
       User user = userMapper.insertoff(usersname);
        sqlSession.commit();
        sqlSession.close();
        return user;
    }
    public User selectLogin(User user){//登录
        SqlSession sqlSession = Sql.openSeeion();
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        User user1 = userMapper.selectLogin(user);
        sqlSession.commit();
        sqlSession.close();
        return user1;
    }
    public void insertUser(User user){//注册
        SqlSession sqlSession = Sql.openSeeion();
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        userMapper.insertUser(user);
        sqlSession.commit();
        sqlSession.close();
    }
    public void deleteByid(int id){
        SqlSession sqlSession = Sql.openSeeion();
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        userMapper.deleteByid(id);
        sqlSession.commit();
        sqlSession.close();

    }
    public PageInfo<User> getPageList(int pageNum,int pageSize){

        PageHelper.startPage(pageNum,pageSize);
        UserService service = new UserService();
        List<User> users=service.selectuserAll();
        PageInfo<User> userPageInfo=new PageInfo<>(users);
        return userPageInfo;
    }

}
